package com.itheima.mp.domain.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("用户接口")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @PostMapping
    @ApiOperation("用户登录接口")
    public void insert(@RequestBody UserFormDTO userFormDTO) {
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable @ApiParam("删除用户id") Long id) {
        userService.removeById(id);
    }

    @GetMapping("/{id}")
    public UserVO getById(@PathVariable @ApiParam("查询用户id") Long id) {
        return userService.selectUserAndAddresses(id);
    }

    @GetMapping
    public List<UserVO> users(@RequestParam("ids") List<Long> ids) {
        return userService.selectListAndAddresses(ids);
    }

    @ApiOperation("根据id扣减余额")
    @PutMapping("/{id}/deduction/{money}")
    public void update(@PathVariable Long id, @PathVariable Integer money) {
        userService.updateMoney(id, money);
    }

    @ApiOperation("条件查询")
    @GetMapping("/list")
    public List<User> select(UserQuery userQuery) {
        return userService.selectList(userQuery);

    }

    @ApiOperation("条件分页查询")
    @GetMapping("/page")
    public PageDTO<UserVO> pageSelect(UserQuery userQuery) {
        return userService.queryUserPage(userQuery);
    }
}
